package fr.ups.jim.isi.tp6;

import fr.lri.swingstates.canvas.CExtensionalTag;
import fr.lri.swingstates.canvas.CText;
import fr.lri.swingstates.canvas.Canvas;
import fr.lri.swingstates.canvas.CStateMachine;
import fr.lri.swingstates.canvas.CShape;
import fr.lri.swingstates.sm.StateMachineListener;
import fr.lri.swingstates.debug.StateMachineVisualization;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.EventObject;

/**
 * @author Nicolas Roussel (roussel@lri.fr)
 * 
 */
public class GraphicalEditor extends JFrame implements StateMachineListener {

	private Canvas canvas;
//	private CShape platte;
//	private ArrayList<CStateMachine> tools;
	private RectangleTool tool;
//	private SelectionTool selector;

	class ToolTag extends CExtensionalTag {
		private CStateMachine tool;

		ToolTag(CStateMachine csm) {
			tool = csm;
		}

		CStateMachine getTool() {
			return tool;
		}
	}

	public GraphicalEditor(String title, int width, int height) {
		super(title);
		canvas = new Canvas(width, height);
		getContentPane().add(canvas);

		tool = new RectangleTool(CStateMachine.BUTTON1, 
				CStateMachine.NOMODIFIER);
//		tools = new ArrayList<CStateMachine>();
//		selector = new SelectionTool(CStateMachine.BUTTON1, CStateMachine.SHIFT);
//		selector.attachTo(canvas) ;
		
//		tools.add(selector);

//		CStateMachine toolRec = new RectangleTool(CStateMachine.BUTTON1,
//				CStateMachine.NOMODIFIER);
//		CStateMachine toolElip = new EllipseTool(CStateMachine.BUTTON1,
//				CStateMachine.NOMODIFIER);
//		CStateMachine toolLine = new LineTool(CStateMachine.BUTTON1,
//				CStateMachine.NOMODIFIER);
//		CStateMachine toolPath = new PathTool(CStateMachine.BUTTON1,
//				CStateMachine.NOMODIFIER);
//		tools.add(toolRec);
//		tools.add(toolElip);
//		tools.add(toolLine);
//		tools.add(toolPath);

//		final int iconSize = 50;
//		platte = canvas.newRectangle(20, 5, tools.size() * iconSize, 15);
//
//		CText platteTitle = canvas.newText(0, 0, "TOOLS", new Font("verdana",
//				Font.PLAIN, 12));
//		platteTitle.setParent(platte);
//		platteTitle.setReferencePoint(0.5, 0.5).translateTo(
//				platte.getCenterX(), platte.getCenterY());

		tool.attachTo(canvas) ;
//		tool.addStateMachineListener(this) ;

//		JFrame smviz = new JFrame("StateMachine Viz");
//		Container pane = smviz.getContentPane();
//		pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
//		pane.add(new JLabel("SelectionTool"));
//		pane.add(new StateMachineVisualization(selector));
//		pane.add(new JLabel("RectangleTool"));
//		pane.add(new StateMachineVisualization(tool)) ;
//		smviz.pack();
//		smviz.setVisible(true);

		pack();
		setVisible(true);
	}

	public void eventOccured(EventObject e) {
		ShapeCreatedEvent csce = (ShapeCreatedEvent) e;
//		csce.getShape().addTag(selector.getMovableTag()).setFillPaint(
//				Color.white);
	}

	public static void main(String[] args) {
		GraphicalEditor editor = new GraphicalEditor("Graphical Editor", 400,
				600);
		editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
